import {Injectable} from '@angular/core';
import {Router, CanActivate, ActivatedRouteSnapshot} from '@angular/router';

@Injectable()
export class AuthGuard implements CanActivate {

  constructor(private router: Router) {
  }

  canActivate(next: ActivatedRouteSnapshot) {
    if (sessionStorage.getItem('token')) {
      // 登录用户
      if (next.data.role === 'admin' && sessionStorage.getItem('role') !== '1') {
        this.router.navigate(['/admin/dashboard']);
        return false;
      }
      return true;
    }
    // 非法访问
    this.router.navigate(['/auth/login']);
    return false;
  }
}
